﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BioBooking.Data.CtrLag
{
    public class ShowingController
    {
        BioBookingContainer container;

        public ShowingController()
        {
            container = new BioBookingContainer();
        }

        public List<Showing> getMoviesFromShowtime(int movieID)
        {
            return container.ShowingSet.Where(p => p.Movie.Id == movieID).ToList();
        }

        public List<Showing> getAllShowings()
        {
            return container.ShowingSet.ToList();
        }

        public Showing getShow(int showingID)
        {
            return container.ShowingSet.Where(p => p.Id == showingID).SingleOrDefault();
        }

        public List<Cinema> getCinemas()
        {
            return container.CinemaSet.ToList();
        }

        public void CreateShowingObject(Showing showing)
        {
            //Removes time from date property
            showing.Date = showing.StartTime.Date;

            Cinema cinema = container.CinemaSet.FirstOrDefault(p => p.Id == showing.CinemaId);
            foreach (Seat seat in cinema.Seats)
            {
                seat.SeatState.Add(new SeatState() { Occupied = true, Showing = showing });
            }

            container.ShowingSet.AddObject(showing);
            container.SaveChanges();
        }

        public void createShowing(DateTime startTime, DateTime endTime, decimal unitPrice, DateTime date, Movie movie, Cinema cinema)
        {
            Showing s = new Showing();
            s.StartTime = startTime;
            s.EndTime = endTime;
            s.UnitPrice = unitPrice;
            s.Date = date;

            s.CinemaId = cinema.Id;
            s.MovieId = movie.Id;

            CreateShowingObject(s);
        }
    }
}
